package com.museum.guide.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.museum.guide.common.result.Result;
import com.museum.guide.entity.Appointment;
import com.museum.guide.service.AppointmentService;
import com.museum.guide.vo.TimeSlotStatistics;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Resource
    private AppointmentService appointmentService;

    /**
     * 分页查询预约信息
     */
    @GetMapping("/page")
    public Result<Page<Appointment>> page(
            Page<Appointment> page,
            String visitorName,
            String status,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            Long userId) {
        return Result.success(appointmentService.page(page, visitorName, status, startDate, endDate, userId));
    }

    /**
     * 获取用户的预约列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<Appointment>> getUserAppointments(@PathVariable Long userId) {
        return Result.success(appointmentService.getUserAppointments(userId));
    }

    /**
     * 新增预约
     */
    @PostMapping("/add")
    public Result<Void> save(@RequestBody @Valid Appointment appointment) {
        appointmentService.saveAppointment(appointment);
        return Result.success();
    }

    /**
     * 更新预约信息
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody @Valid Appointment appointment) {
        appointmentService.updateAppointment(appointment);
        return Result.success();
    }

    /**
     * 取消预约
     */
    @PostMapping("/cancel/{id}")
    public Result<Void> cancel(@PathVariable Long id) {
        appointmentService.cancelAppointment(id);
        return Result.success();
    }

    /**
     * 审核预约
     */
    @PostMapping("/review/{id}")
    public Result<Void> review(
            @PathVariable Long id,
            @RequestParam String status,
            @RequestParam(required = false) String remark) {
        appointmentService.reviewAppointment(id, status, remark);
        return Result.success();
    }

    /**
     * 根据日期查询不同时间段已预约的人数
     */
    @GetMapping("/statistics/timeSlot")
    public Result<List<TimeSlotStatistics>> getTimeSlotStatistics(
            String date) {
        return Result.success(appointmentService.getTimeSlotStatistics(date));
    }
}